/*
 * Copyright (c) 2018. J4dream Created.
 */

package com.j4dream.property.leetcode.basic.design;


import java.util.Random;

public class ShuffleArray {
    private int[] origin;
    private int[] curr;

    private Random rand = new Random();

    public ShuffleArray(int[] nums) {
        this.origin = nums;
    }

    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        return this.origin;
    }

    private int randomIndex(int min, int max) {
        return rand.nextInt(max - min) + min;
    }

    private void swap(int x, int y) {
        int t = curr[x];
        curr[x] = curr[y];
        curr[y] = t;
    }

    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        curr = origin.clone();
        for (int i = 0; i < curr.length; i++) {
            swap(i, randomIndex(i, curr.length));
        }

        return this.curr;
    }

    public static void main(String args[]) {
        int[] arr = new int[] {1};

        ShuffleArray solution = new ShuffleArray(arr);
        solution.reset();
        solution.shuffle();
    }
}
